package ge.unihack.instantdj.utils;

import java.io.InputStream;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.graphics.Paint;
import android.os.SystemClock;
import android.view.View;

public class GifView extends View {

	Movie movie;
    InputStream is;
    long moviestart;
    Bitmap originalBitmap;
    Paint p;
    
    public GifView(Context context, int resourceId) {   

        super(context);         
        
        is=context.getResources().openRawResource(resourceId);		          
        movie=Movie.decodeStream(is);
        p = new Paint();
		p.setAntiAlias(true);
    }

	@SuppressLint("NewApi")
	@Override
	protected void onDraw(Canvas canvas) {
	
	    canvas.drawColor(Color.BLACK);
	    super.onDraw(canvas);
	    long now=SystemClock.uptimeMillis();
	    
		if (moviestart == 0) { 
		    moviestart = now;
		}
		
		setLayerType(View.LAYER_TYPE_SOFTWARE, p);
		int relTime = (int)((now - moviestart) % movie.duration());
		movie.setTime(relTime);
		movie.draw(canvas,0,getWidth()/2);

		this.invalidate();
	}
}
